/*
 * CbrtatorView.java
 */
package cbrtator;

import cbrtator.graph.visualisation;
import java.io.IOException;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import opennlp.grokkit.gui.ExtensionFileFilter;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;


import rapc.EnsembleCas;
import rapc.cas;
import rapc.descripteur;

/**
 * The application's main frame.
 */
public class CbrtatorView extends FrameView {

    //répertoire de travail d'un fichier.(variable global)
    public static  String Reptravail=null;
    
    private ArrayList<descripteur> ldescripteur = new ArrayList<descripteur>();
    private ArrayList<cas> listecas = new ArrayList<cas>();

    public CbrtatorView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);


        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }

                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);

                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());

                }
            }
        });
    }

    @Action
    public void showAboutBox() {


        if (aboutBox == null) {
            JFrame mainFrame = CbrtatorApp.getApplication().getMainFrame();
            aboutBox = new CbrtatorAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);                     
        }
        CbrtatorApp.getApplication().show(aboutBox);
    }




    @Action
    public void gestiondescr() {
        this.Pcontenant.removeAll();
        DescripteurManage fenetre = new DescripteurManage(ldescripteur);


        this.Pcontenant.add(fenetre);

        fenetre.setBounds(
                0, 0, Pcontenant.getWidth(), Pcontenant.getHeight());

        this.Pcontenant.updateUI();

    }
    
       @Action
    public void gestionCas() {
        this.Pcontenant.removeAll();
        descripteurcas fenetre = new descripteurcas(ldescripteur,listecas);


        this.Pcontenant.add(fenetre);

        fenetre.setBounds(
                0, 0, Pcontenant.getWidth(), Pcontenant.getHeight());

        this.Pcontenant.updateUI();

    }


    
       //ouvre une fentre pour choisir un fichier cas
    @Action
    public void RechercheCas() throws IOException {
        
        
        this.Pcontenant.removeAll();
        CasSource fenetre = new CasSource(ldescripteur,listecas);


        this.Pcontenant.add(fenetre);

        fenetre.setBounds(
                0, 0, Pcontenant.getWidth(), Pcontenant.getHeight());

        this.Pcontenant.updateUI();      

    }

    //ouvre une fentre pour enregistrer un fichier cas
    @Action
    public void saveforfile() throws IOException {

        FileSystemView vueSysteme = FileSystemView.getFileSystemView();
        //récupération des répertoires
        File home = vueSysteme.getHomeDirectory();
        //mise en place d'un filtre sur les fichiers
        FileFilter tatorfilter = new ExtensionFileFilter("ttr");

        JFileChooser homeChooser = new JFileChooser(home);
        homeChooser.addChoosableFileFilter(tatorfilter);
        
        
        if (homeChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) 
         {
             //on sauvegarde
             EnsembleCas mes = new EnsembleCas(listecas);
             mes.save(homeChooser.getSelectedFile().getAbsoluteFile()+".ttr");
             Reptravail=homeChooser.getSelectedFile().getAbsoluteFile()+".ttr";      
         }
    }
    
    
       @Action
    public void savefile() throws IOException {
              
        
        if (Reptravail!=null) 
         {
             //on sauvegarde
             EnsembleCas mes = new EnsembleCas(listecas);
             mes.save(Reptravail);
                   
         }
        else{ saveforfile();}
    }
    
    
        //ouvre une fentre pour ouvrir et charger un fichier cas
    @Action
    public void openforfile() throws IOException {

        FileSystemView vueSysteme = FileSystemView.getFileSystemView();
        //récupération des répertoires
        File home = vueSysteme.getHomeDirectory();
        //mise en place d'un filtre sur les fichiers
        FileFilter tatorfilter = new ExtensionFileFilter("ttr");

        JFileChooser homeChooser = new JFileChooser(home);
        homeChooser.addChoosableFileFilter(tatorfilter);
        
        
        if (homeChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) 
         {
             //on charge
             EnsembleCas mes = new EnsembleCas(listecas);
             mes.open(homeChooser.getSelectedFile().getAbsoluteFile().toString());
             
             ldescripteur=listecas.get(0).getDescripteur();             
             Reptravail=homeChooser.getSelectedFile().getAbsoluteFile().toString();
             
         }



    }
    
    
    
    

    //affiche des cas
    @Action
    public void affiche() {

       

        for (int i = 0; i < ldescripteur.size(); i++) {
            System.out.println(ldescripteur.get(i).toString());

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        barreoutil = new javax.swing.JToolBar();
        AddDescripteur = new javax.swing.JButton();
        Ngraphe = new javax.swing.JButton();
        AjoutCas = new javax.swing.JButton();
        RechercheCas = new javax.swing.JButton();
        Pcontenant = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        ouvrircbr = new javax.swing.JMenuItem();
        enregistrer = new javax.swing.JMenuItem();
        enregistrersous = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();

        mainPanel.setName("mainPanel"); // NOI18N

        barreoutil.setRollover(true);
        barreoutil.setMaximumSize(new java.awt.Dimension(23, 23));
        barreoutil.setMinimumSize(new java.awt.Dimension(23, 23));
        barreoutil.setName("barreoutil"); // NOI18N
        barreoutil.setPreferredSize(new java.awt.Dimension(23, 23));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cbrtator.CbrtatorApp.class).getContext().getActionMap(CbrtatorView.class, this);
        AddDescripteur.setAction(actionMap.get("gestiondescr")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cbrtator.CbrtatorApp.class).getContext().getResourceMap(CbrtatorView.class);
        AddDescripteur.setText(resourceMap.getString("AddDescripteur.text")); // NOI18N
        AddDescripteur.setFocusable(false);
        AddDescripteur.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AddDescripteur.setName("AddDescripteur"); // NOI18N
        AddDescripteur.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        barreoutil.add(AddDescripteur);

        Ngraphe.setText(resourceMap.getString("Ngraphe.text")); // NOI18N
        Ngraphe.setActionCommand(resourceMap.getString("Ngraphe.actionCommand")); // NOI18N
        Ngraphe.setFocusable(false);
        Ngraphe.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        Ngraphe.setName("Ngraphe"); // NOI18N
        Ngraphe.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        Ngraphe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NgrapheActionPerformed(evt);
            }
        });
        barreoutil.add(Ngraphe);
        Ngraphe.getAccessibleContext().setAccessibleName(resourceMap.getString("NewGraphe.AccessibleContext.accessibleName")); // NOI18N

        AjoutCas.setAction(actionMap.get("gestionCas")); // NOI18N
        AjoutCas.setText(resourceMap.getString("ajoutcas.text")); // NOI18N
        AjoutCas.setFocusable(false);
        AjoutCas.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AjoutCas.setName("ajoutcas"); // NOI18N
        AjoutCas.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        barreoutil.add(AjoutCas);

        RechercheCas.setAction(actionMap.get("RechercheCas")); // NOI18N
        RechercheCas.setText(resourceMap.getString("RechercheCas.text")); // NOI18N
        RechercheCas.setFocusable(false);
        RechercheCas.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        RechercheCas.setName("RechercheCas"); // NOI18N
        RechercheCas.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        barreoutil.add(RechercheCas);

        Pcontenant.setAlignmentX(0.0F);
        Pcontenant.setAlignmentY(0.0F);
        Pcontenant.setName("Pcontenant"); // NOI18N

        javax.swing.GroupLayout PcontenantLayout = new javax.swing.GroupLayout(Pcontenant);
        Pcontenant.setLayout(PcontenantLayout);
        PcontenantLayout.setHorizontalGroup(
            PcontenantLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 870, Short.MAX_VALUE)
        );
        PcontenantLayout.setVerticalGroup(
            PcontenantLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 469, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(barreoutil, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 880, Short.MAX_VALUE)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(Pcontenant, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(barreoutil, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Pcontenant, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setAction(actionMap.get("NewCbr")); // NOI18N
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        ouvrircbr.setAction(actionMap.get("openforfile")); // NOI18N
        ouvrircbr.setText(resourceMap.getString("ouvrircbr.text")); // NOI18N
        ouvrircbr.setName("ouvrircbr"); // NOI18N
        fileMenu.add(ouvrircbr);

        enregistrer.setAction(actionMap.get("savefile")); // NOI18N
        enregistrer.setText(resourceMap.getString("enregistrer.text")); // NOI18N
        enregistrer.setName("enregistrer"); // NOI18N
        fileMenu.add(enregistrer);

        enregistrersous.setAction(actionMap.get("saveforfile")); // NOI18N
        enregistrersous.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        enregistrersous.setText(resourceMap.getString("enregistrersous.text")); // NOI18N
        enregistrersous.setName("enregistrersous"); // NOI18N
        fileMenu.add(enregistrersous);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 880, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 860, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void NgrapheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NgrapheActionPerformed
        this.Pcontenant.removeAll();
        //visualisation v = new visualisation(500,400);
        visualisation v = new visualisation(Pcontenant.getWidth() - 50, Pcontenant.getHeight() - 50);
        this.Pcontenant.add(v);

        v.setBounds(
                0, 0, Pcontenant.getWidth(), Pcontenant.getHeight());
        this.Pcontenant.updateUI();
    }//GEN-LAST:event_NgrapheActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddDescripteur;
    private javax.swing.JButton AjoutCas;
    private javax.swing.JButton Ngraphe;
    private javax.swing.JPanel Pcontenant;
    private javax.swing.JButton RechercheCas;
    private javax.swing.JToolBar barreoutil;
    private javax.swing.JMenuItem enregistrer;
    private javax.swing.JMenuItem enregistrersous;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem ouvrircbr;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
}
